//
//  PopAnimatorTool.swift
//  WBSwift
//
//  Created by maker on 2016/11/10.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit

class PopAnimatorTool: NSObject {
    var transFrame : CGRect = CGRect.zero
    
    var isShow = false
    var callBack : ((_ isShow : Bool) -> ())?
    init(callBack : @escaping (_ isShow : Bool) -> ()){
        self.callBack = callBack
    }
}
//MARK:==========转场代理==========
extension PopAnimatorTool:UIViewControllerTransitioningDelegate{
    
    //自定义要弹出的 view, 并在其中改变大小
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{
        let presentView = PresentVC(presentedViewController: presented, presenting: presenting)
        presentView.transFrame = transFrame
        return presentView
    }
    
    //自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isShow = true
        callBack?(isShow)
        return self;
    }
    
    //自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isShow = false
        callBack?(isShow)
        return self;
    }
}

//MARK:========== 转场动画协议中方法 ==========
extension PopAnimatorTool:UIViewControllerAnimatedTransitioning {
    
    //协议中必须实现的两个方法
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
        
        return 0.5
    }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        isShow ? animateForPresentView(transitionContext) : animateForDismissView(transitionContext)
    }
    
    //弹出动画
    fileprivate func animateForPresentView(_ transitionContext: UIViewControllerContextTransitioning) {
        //1.transitionContext 从转场上下文中可以获取到要弹出的 view
        //UITransitionContextFromViewKey,  UITransitionContextToViewKey
        let presentView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        //2.把弹出视图手动加入到容器中
        transitionContext.containerView.addSubview(presentView)
        
        //3.给弹出视图做动画
        presentView.transform = CGAffineTransform(scaleX: 1, y: 0)
        presentView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        UIView .animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentView.transform = CGAffineTransform.identity
        }, completion: { (isFinish) in
            transitionContext.completeTransition(true)
        }) 
        
    }
    //消失动画
    fileprivate func animateForDismissView(_ transitionContext: UIViewControllerContextTransitioning) {
        //1.获取要消失的 view
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!

        //2.做动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            dismissView.transform = CGAffineTransform(scaleX: 1, y: 0.0001)
            
        }, completion: { (_) in
            dismissView .removeFromSuperview()
            transitionContext.completeTransition(true)
        }) 
    }
    
}
